//
//  CRMListTopView.m
//  New_Runway
//
//  Created by 邢毅 on 2018/4/11.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CRMListTopView.h"

@implementation CRMListTopView
{
    NSInteger _baseTag;
    NSArray *_btnArr;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _baseTag = 999;
    }
    return self;
}

- (void)prepar {
    
    NSInteger count = [self getTitle].count;
    
    NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:count];
    
    for (NSInteger i = 0; i < count; i++) {
        
        UIButton *btn = DPLayoutButton(10, DPBtnLayoutTypeLeftTitleRightImage)
        .setNormalImage(getImageFromName(@"triangle"))
        .setNormalTitle([self getTitle][i])
        .setTitleFont(kPingFangRegular12())
        .setNormalTitleColor(getColorFromString(@"323232"))
        .addClickAction(self, @selector(itemBtnDidClick:));
        
        [arrM addObject:btn];
        self.addSubview(btn);
    }
    
    _btnArr = arrM.copy;
    
    [self layoutUI];
}

- (void)layoutUI {
    
    [_btnArr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    
    [_btnArr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.height.equalTo(self.mas_height);
    }];
}

- (void)itemBtnDidClick:(UIButton *)sender {
    
    CRMListTopViewButtonType btnType = 0;
    
    if ([sender.titleLabel.text isEqualToString:@"排序"]) {
        btnType = CRMListTopViewButtonTypeSort;
    } else if ([sender.titleLabel.text isEqualToString:@"级别"]) {
        btnType = CRMListTopViewButtonTypeLevel;
    } else if ([sender.titleLabel.text isEqualToString:@"来源"]) {
        btnType = CRMListTopViewButtonTypeChannel;
    } else if ([sender.titleLabel.text isEqualToString:@"状态"]) {
        btnType = CRMListTopViewButtonTypeState;
    } else if ([sender.titleLabel.text isEqualToString:@"筛选"]) {
        btnType = CRMListTopViewButtonTypeFilter;
    }
    
    if ([self.delegate respondsToSelector:@selector(topView:buttonDidClick:withType:)]) {
        [self.delegate topView:self buttonDidClick:sender withType:btnType];
    }
}

- (NSArray <NSString *>*)getTitle {
    
    if (self.listViewType == kCRMListViewTypeNewToAssigned) {
        return @[@"排序", @"级别", @"来源"];
    } else if (self.listViewType == kCRMListViewTypeStaffNotProcessed ||
               self.listViewType == kCRMListViewTypeNewAssignedPending ||
               self.listViewType == kCRMListViewTypeTodayPending ||
               self.listViewType == kCRMListViewTypeExpiredUnprocessed) {
        return @[@"排序", @"级别", @"来源", @"筛选"];
    } else {
        return @[@"排序", @"级别", @"来源", @"状态", @"筛选"];
    }
}

@end
